package com.saasitems.admin.service;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.saasitems.admin.dao.MattsMapper;
import com.saasitems.admin.dao.MediaResMapper;
import com.saasitems.admin.model.*;
import com.saasitems.admin.web.common.UtilFromDb;
import com.saasitems.admin.web.vo.matts.MattsQueryVO;
import com.saasitems.admin.web.vo.matts.MattsVO;
import com.saasitems.admin.web.vo.rest.matts.RestMattsInfoVO;
import com.saasitems.admin.web.vo.rest.original.RestOriginalInfoVO;
import com.saasitems.common.model.PageInfo;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * Created by Administrator on 2017/4/11.
 */
@Service
public class MattsService {
    @Autowired
    private MattsMapper mattsMapper;

    @Autowired
    private MediaResMapper mediaResMapper;

    public List<Matts> query(MattsQueryVO queryVO, PageInfo page) {
        MattsExample ex = new MattsExample();
        MattsExample.Criteria c = ex.createCriteria();
        if (queryVO != null) {
            //    c.andTitleLike("%" + queryVO.getTitle() + "%");
            c.andOriginalIdEqualTo(queryVO.getOriginalId());

            if (StringUtils.isNotBlank(queryVO.getDspTxt())) {
                c.andDspTxtLike("%" + queryVO.getDspTxt() + "%");
            }
        }

        if (page != null) {
            PageHelper.startPage(page.getPageNo(), page.getPageSize());
        }
        List<Matts> list = this.mattsMapper.selectByExample(ex);
        if (page != null) {
            // 取count的值
            page.setTotalCount((long)((Page) list).getTotal());
        }
        return list;
    }

    @Transactional(readOnly = false)
    public void save(MattsVO vo) {
        Matts o = new Matts();
        o.setOriginalId(vo.getOriginalId());
        o.setDspTxt(vo.getDspTxt());
        o.setDspSubTxt(vo.getDspSubTxt());
        o.setRecType(vo.getRecType());
        o.setRecId(vo.getRecId());
        o.setDspSort(vo.getDspSort());
        o.setDelFlg(0);
        o.setCreateTime(new Date());
        mattsMapper.insert(o);
    }

    /**
     * 通过主键获得产品信息
     * @param id
     * @return
     */
    public MattsVO getInfoById(Integer id) {
        Matts p = mattsMapper.selectByPrimaryKey(id);
        MattsVO vo = new MattsVO();
        BeanUtils.copyProperties(p, vo);

        if (StringUtils.isNotBlank(vo.getRecId())) {
            MediaRes ms = mediaResMapper.selectByPrimaryKey(vo.getRecId());
            vo.setPicName(ms.getFileName());
        }
        return vo;
    }

    public void update(MattsVO vo) {
        Matts p = mattsMapper.selectByPrimaryKey(vo.getId());

        p.setOriginalId(vo.getOriginalId());
        p.setDspTxt(vo.getDspTxt());
        p.setDspSubTxt(vo.getDspSubTxt());
        p.setRecType(vo.getRecType());
        p.setRecId(vo.getRecId());
        p.setBkRecId(vo.getBkRecId());
        p.setDspSort(vo.getDspSort());
        p.setCreateTime(new Date());

        mattsMapper.updateByPrimaryKey(p);
    }

    public List<RestMattsInfoVO> getInfoByOriginalId(Integer originalId) {
        MattsExample ex = new MattsExample();
        MattsExample.Criteria c = ex.createCriteria();
        c.andOriginalIdEqualTo(originalId);
        c.andDelFlgEqualTo(0);

        ex.setOrderByClause("dsp_sort asc, create_time asc");
        List<RestMattsInfoVO> rets = new ArrayList<RestMattsInfoVO>();
        List<Matts> mts = mattsMapper.selectByExample(ex);
        for (Matts p : mts) {
            RestMattsInfoVO vo = new RestMattsInfoVO();

            vo.setKey(String.valueOf(p.getId()));
            vo.setDspTxt(p.getDspTxt());
            vo.setDspSubTxt(p.getDspSubTxt());
            if (StringUtils.isNotBlank(p.getRecId())) {
                MediaRes ms = mediaResMapper.selectByPrimaryKey(p.getRecId());
                String path = UtilFromDb.getPathFromMS(ms);
                vo.setPic(path);
            }
            rets.add(vo);
        }
        return rets;
    }
}
